﻿using UnityEngine.UI;
using LuaFramework;
using System.Collections.Generic;

namespace Guan
{
    public class UpdateResourcesView : View
    {
        private string message;

        ///<summary>
        /// 监听的消息
        ///</summary>
        List<string> MessageList {
            get {
                return new List<string>()
            {
                NotiConst.UPDATE_MESSAGE,
                NotiConst.UPDATE_EXTRACT,
                NotiConst.UPDATE_DOWNLOAD,
                NotiConst.UPDATE_PROGRESS,
            };
            }
        }

        void Awake()
        {
            RemoveMessage(this, MessageList);
            RegisterMessage(this, MessageList);
        }

        /// <summary>
        /// 处理View消息
        /// </summary>
        /// <param name="message"></param>
        public override void OnMessage(IMessage message)
        {
            string name = message.Name;
            object body = message.Body;
            switch (name)
            {
                case NotiConst.UPDATE_MESSAGE:      //更新消息
                    UpdateMessage(body.ToString());
                    break;
                case NotiConst.UPDATE_EXTRACT:      //更新解压
                    UpdateExtract(body.ToString());
                    break;
                case NotiConst.UPDATE_DOWNLOAD:     //更新下载
                    UpdateDownload(body.ToString());
                    break;
                case NotiConst.UPDATE_PROGRESS:     //更新下载进度
                    UpdateProgress(body.ToString());
                    break;
            }
        }

        public void UpdateMessage(string data)
        {
            this.message = data;
            //text += "UpdateMessage\t" + data + "\n";
        }
        public void UpdateExtract(string data)
        {
            this.message = data;
            string[] strs = data.Split('_');
            //this.downloadSpeed = strs[0];
            this.downloadProgress = strs[strs.Length-1];
            //text += "UpdateExtract\t" + data + "\n";
        }
        public void UpdateDownload(string data)
        {
            this.message = data;
            downloadProgress = "0";
            //text += "UpdateDownload\t" + data + "\n";
        }
        public void UpdateProgress(string data)
        {
            string[] strs = data.Split('_');
            this.downloadSpeed = strs[0];
            this.downloadProgress = strs[1];
            //text += "UpdateProgress\t" + data + "\n";
        }
        private string downloadSpeed;
        private string downloadProgress;

        public Text downloadSpeedText;
        public Text messageText;
        public Text progressText;
        public Text downSpeedText;
        public Slider slider;
        float result;
        private void Update()
        {
            messageText.text = message;
            float.TryParse(downloadProgress, out result);
            downloadSpeedText.text = downloadSpeed;
            slider.value = result;
            progressText.text = result*100+"%";
            downSpeedText.text = downloadSpeed;
        }
    }
}